package com.lightboat.task;

import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.lightboat.common.utils.DateUtils;
import com.lightboat.common.utils.PageUtils;
import com.lightboat.common.utils.StringUtils;
import com.lightboat.system.domain.CrmCustomer;
import com.lightboat.system.domain.CrmUserFollowRecord;
import com.lightboat.system.service.ICrmCustomerService;
import com.lightboat.system.service.ICrmUserFollowRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author lightboat
 */
@Slf4j
@RequiredArgsConstructor
@Component("qxzTask")
public class QxzTask {

    private final ICrmUserFollowRecordService crmUserFollowRecordService;
    private final ICrmCustomerService crmCustomerService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    /**
     * 跟进置顶任务
     */
    public void followUpToTheTopTask() {
        LambdaQueryWrapper<CrmUserFollowRecord> queryWrapper = Wrappers.<CrmUserFollowRecord>lambdaQuery()
                .eq(CrmUserFollowRecord::getIsFollowed, 0)
                .last("and contact_next_time BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_ADD(NOW(), INTERVAL 30 MINUTE)");
                // .between(CrmUserFollowRecord::getContactNextTime,"DATE_SUB(NOW(), INTERVAL 30 MINUTE)","DATE_ADD(NOW(), INTERVAL 30 MINUTE)");
        long count = crmUserFollowRecordService.count(queryWrapper);
        queryWrapper.select(CrmUserFollowRecord::getId,CrmUserFollowRecord::getUserId,CrmUserFollowRecord::getCustomerId,CrmUserFollowRecord::getUsageEndTime);
        queryWrapper.groupBy(CrmUserFollowRecord::getCustomerId);
        long l = count / 1000;
        for (int i = 0; i < l; i++) {
            PageUtils.startPage(1, 1000);
            List<CrmUserFollowRecord> records = crmUserFollowRecordService.list(queryWrapper);
            PageUtils.clearPage();
            List<Long> customerIds = records.parallelStream().map(CrmUserFollowRecord::getCustomerId).toList();
            List<CrmCustomer> crmCustomers = crmCustomerService.listByIds(customerIds);
            crmCustomers.parallelStream().forEach(item -> {
                CrmCustomer customer = new CrmCustomer();
                customer.setCustomerId(item.getCustomerId());
                customer.setTopStatus("1");
                customer.setTopTime(DateUtils.getNowDate());
                List<String> topTypes = Opt.ofNullable(item.getTopType()).map(s -> StrUtil.split(s, ",")).peek(ls -> {
                    ls.add("2");
                }).orElseGet(() -> Lists.newArrayList("2")).stream().distinct().toList();
                String joinTopType = StrUtil.join(",", topTypes);
                customer.setTopType(joinTopType);
                crmCustomerService.updateById(customer);
            });

        }
    }

    /**
     * 用量结束置顶任务
     */
    public void usageEndTimeToTheTopTask() {
        QueryWrapper<CrmUserFollowRecord> queryWrapper = Wrappers.<CrmUserFollowRecord>query()
                .last("where usage_end_time BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_ADD(NOW(), INTERVAL 30 MINUTE)");
        long count = crmUserFollowRecordService.count(queryWrapper);
        queryWrapper.select("id","user_id","customer_id","usage_end_time");
        queryWrapper.groupBy("customer_id");
        long l = count / 1000;
        for (int i = 0; i < l; i++) {
            PageUtils.startPage(1, 1000);
            List<CrmUserFollowRecord> records = crmUserFollowRecordService.list(queryWrapper);
            PageUtils.clearPage();
            List<Long> customerIds = records.parallelStream().map(CrmUserFollowRecord::getCustomerId).toList();
            List<CrmCustomer> crmCustomers = crmCustomerService.listByIds(customerIds);
            crmCustomers.parallelStream().forEach(item -> {
                CrmCustomer customer = new CrmCustomer();
                customer.setCustomerId(item.getCustomerId());
                customer.setTopStatus("1");
                customer.setTopTime(DateUtils.getNowDate());
                List<String> topTypes = Opt.ofNullable(item.getTopType()).map(s -> StrUtil.split(s, ",")).peek(ls -> {
                    ls.add("1");
                }).orElseGet(() -> Lists.newArrayList("1")).stream().distinct().toList();
                String joinTopType = StrUtil.join(",", topTypes);
                customer.setTopType(joinTopType);
                crmCustomerService.updateById(customer);
            });

        }
    }
}
